Ext.define('Admin.view.process.model.ProcessModelViewController', {
    extend: Ext.app.ViewController,
    alias: 'controller.processmodel',

    onClickProcessModelGridAddButton: function(btn) {
        Ext.create(Ext.apply({ xtype: 'processmodel-create-window' }));
    },

    onClickProcessModelCreateFormCancelButton: function(btn) {
        var win = btn.up('window');
        if (win) {
            win.close();
        }
    },

    onClickProcessModelCreateFormSubmitButton: function(btn) {
        var form = btn.up('form');
        form.getForm().submit({
            url: 'model/create',
            headers: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(form, action) {
                Ext.Msg.alert('系统提示', "创建成功!", function() {
                    btn.up('window').close();
                    Ext.getCmp('processmodel-grid').getStore().reload();
                    window.open(action.result.msg);
                });
            },
            failure: function(form, action) {
                Ext.Msg.alert('系统提示', action.result.msg);
            }
        });
    },

    onClickProcessModelGridDeployButton: function(view, recIndex, cellIndex, item, e, record) {
        var csrf = document.getElementsByTagName('meta')['_csrf'].content;

        Ext.Ajax.request({
            url: 'model/deploy/' + record.get('id'),
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": csrf
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg, function() {
                        Ext.getCmp('processmodel-grid').getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;
                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';

                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    },

    onClickProcessModelGridDeleteButton: function(view, recIndex, cellIndex, item, e, record) {
        var csrf = document.getElementsByTagName('meta')['_csrf'].content;

        Ext.Ajax.request({
            url: 'model/delete/' + record.get('id'),
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": csrf
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg, function() {
                        Ext.getCmp('processmodel-grid').getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;
                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';

                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    }
});